package com.psl.services.work.waixie;
import com.psl.Dao.Conn;
import com.psl.model.wxbjmodel;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class wxbj
{
  public boolean wxbjadd(String h_id, String h_area, String h_name, String h_size, int h_send, String h_sendtime, int h_receipt,String gx, String h_receipttime,String no)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("insert into wxbj(h_id,h_area,h_name,h_size,h_send,h_sendtime,h_receipt,gx,h_receipttime,no) VALUES (?,?,?,?,?,?,?,?,?,?)");
      pstmt.setString(1, h_id);
      pstmt.setString(2, h_area);
      pstmt.setString(3, h_name);
      pstmt.setString(4, h_size);
      pstmt.setInt(5, h_send);
      pstmt.setString(6, h_sendtime);
      pstmt.setInt(7, h_receipt);
      pstmt.setString(8, gx);
      pstmt.setString(9, h_receipttime);
      pstmt.setString(10,no);
      pstmt.execute();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
  
  public boolean update(int id, int receipt,String h_receipttime)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("update wxbj set h_receipt=?,h_receipttime=? where id=?");
      pstmt.setInt(1, receipt);
      pstmt.setString(2, h_receipttime);
      pstmt.setInt(3, id);
      pstmt.executeUpdate();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }

  public boolean del(int id)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("DELETE from wxbj where id=?");
      pstmt.setInt(1, id);
      pstmt.execute();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }


  
  public List wxbj()
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List ship = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT id,h_id,h_area,h_name,h_size,h_send,h_sendtime,h_receipt,gx,h_receipttime,no from wxbj order by id desc");
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        wxbjmodel wm = new wxbjmodel();
        wm.setId(rs.getInt(1));
        wm.setH_id(rs.getString(2));
        wm.setH_area(rs.getString(3));
        wm.setH_name(rs.getString(4));
        wm.setH_size(rs.getString(5));
        wm.setH_send(rs.getInt(6));
        wm.setH_sendtime(rs.getString(7));
        wm.setH_receipt(rs.getInt(8));
        wm.setGx(rs.getString(9));
        wm.setH_receipttime(rs.getString(10));
        wm.setNo(rs.getString(11));
        ship.add(wm);
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setAttribute("ship",ship);
      }
      return ship;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
  }

  public List detail(String no)
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List ship = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT id,h_id,h_area,h_name,h_size,h_send,h_sendtime,h_receipt,gx,h_receipttime,no from wxbj where no=? ");
      pstmt.setString(1,no);
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        wxbjmodel wm = new wxbjmodel();
        wm.setId(rs.getInt(1));
        wm.setH_id(rs.getString(2));
        wm.setH_area(rs.getString(3));
        wm.setH_name(rs.getString(4));
        wm.setH_size(rs.getString(5));
        wm.setH_send(rs.getInt(6));
        wm.setH_sendtime(rs.getString(7));
        wm.setH_receipt(rs.getInt(8));
        wm.setGx(rs.getString(9));
        wm.setH_receipttime(rs.getString(10));
        wm.setNo(rs.getString(10));
        ship.add(wm);
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setAttribute("Detail",ship);
      }
      return ship;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
  }


  public boolean updateprocstatus(String no)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("UPDATE wxbj set no=? where no=1");
      pstmt.setString(1, no);
      pstmt.executeUpdate();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }



}
